﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iSun.BLL;
using iSun.Model;
using System.Reflection;
using Microsoft.Win32;


namespace iSun.Quartz
{
    public partial class Start : Form
    {
        public Start()
        {
            InitializeComponent();
        }

        private void Start_Load(object sender, EventArgs e)
        {
            this.notifyIcon1.Visible = true;
            notifyIcon1.Text = "任务调度";
            this.notifyIcon1.ContextMenuStrip = contextMenuStrip1;
            //SysScheduler.Instance.Start();
             this.notifyIcon1.Visible = true;
             //SysTask task = new SysTask();
             //task.GroupName = "Start";
             //task.JobName = "StartTask";
             //task.TaskName ="StartTask";
             //task.TriggerName = "StartTrriger";
             //task.CronExp = "30 1/2 * ? * *";
             //task.RunFlag = "1";
             //task.JobType = "iSun.Quartz.RunIssueTask,iSun.Quartz";
             //task.JobStartDate = DateTime.UtcNow;
             
             //string[] _type = task.JobType.Split(',');
             //if (_type.Length > 1)
             //{
             //    string AssemblyName = _type[1];
             //    string TypeName = _type[0];
             //    Assembly assem = Assembly.Load(AssemblyName);
             //    Type t = assem.GetType(TypeName);
             //    //SysScheduler.Instance.Insert(task, t);
             //}
             
             AutoRun();
        }

        private void AutoRun()
        {
            string starupPath = Application.ExecutablePath;
            //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装.
            RegistryKey loca = Registry.LocalMachine;
            RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            try
            {
                //SetValue:存储值的名称
                run.SetValue("WinForm", starupPath);
                //MessageBox.Show(" 注册表添加成功!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                loca.Close();
            }
            catch (Exception e)
            {
                //MessageBox.Show(ee.Message.ToString(), "提 示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //SysScheduler.Instance.Insert();
        }

        private void Start_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.notifyIcon1.Visible = true;
            this.ShowInTaskbar = false;
            e.Cancel = true;
            this.Hide();
            notifyIcon1.Text = "任务调度";
            notifyIcon1.BalloonTipText = "任务调度";
            notifyIcon1.BalloonTipTitle = "任务调度";
            notifyIcon1.ShowBalloonTip(1000);
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Start_MinimumSizeChanged(object sender, EventArgs e)
        {
            this.notifyIcon1.Visible = true;
            this.ShowInTaskbar = false;
            //e.Cancel = true;
            this.Hide();
            notifyIcon1.Text = "任务调度";
            notifyIcon1.BalloonTipText = "任务调度";
            notifyIcon1.BalloonTipTitle = "任务调度";
            notifyIcon1.ShowBalloonTip(1000);
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            // MessageBox.Show("");
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {

        }

        private void contextMenuStrip1_MouseClick(object sender, MouseEventArgs e)
        {
            this.notifyIcon1.Visible = false;
            this.Close();
            this.Dispose();
            //MessageBox.Show(e.Button.ToString());
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.notifyIcon1.Visible = true;
            notifyIcon1.Text = "任务调度";
            this.Hide();
        }
    }
}
